package com.pai4j.search.service.voctor;

import com.pai4j.common.constants.MilvusConstants;
import com.pai4j.common.exception.BusinessException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
/**
 *  向量检索服务工厂类
 *
 * @author: CYM-pai
 * @date: 2025/05/29 11:27
 **/
@Component
public class MilvusServiceFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;
    /**
     * 向量检索服务映射 : biz -> AbstractMilvusService 每个业务对应一个向量检索服务
     **/
    private static Map<String, AbstractMilvusService> services = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        this.registryMilvusSearchService();
    }

    /**
     * 注册向量检索服务
     *
     */
    private void registryMilvusSearchService() {

        services.put(MilvusConstants.SearchBiz.SEARCH_ARTICLE.getBiz(), applicationContext.getBean(ArticleMilvusService.class));
    }

    public static AbstractMilvusService getService(String biz) {
        AbstractMilvusService abstractMilvusService = services.get(biz);
        if (abstractMilvusService != null) {
            return abstractMilvusService;
        }
        throw new BusinessException("不支持的向量检索服务");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
